import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:news_flutter/pages/community//community_index_page.dart';
import 'package:news_flutter/pages/index//news_list_page.dart';
import 'package:news_flutter/pages/user/user_center_page.dart';
import 'package:news_flutter/pages/video//videos_list_page.dart';
import 'package:news_flutter/utils/precompile.dart';

class MainTabbarPage extends StatefulWidget {
  @override
  _MainTabbarPageState createState() => _MainTabbarPageState();
}

class _MainTabbarPageState extends State<MainTabbarPage> with AutomaticKeepAliveClientMixin{
  @override
  bool get wantKeepAlive => true;
  GlobalKey _bottomNavigationBarKey = GlobalKey();

  List<Widget> _pages ;
  int _pageIndex = 0;
  PageController _pageController = PageController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
     _pages = <Widget>[
      NewsListPage(),
      VideoListPage(),
      CommunityIndexPage(),
      UserCenterPage(),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView.builder(itemBuilder: (context,index){
        return _pages[index];
      },
        controller: _pageController,
      ),
      bottomNavigationBar:  BottomNavigationBar(
        key: _bottomNavigationBarKey,
        type: BottomNavigationBarType.fixed,
        unselectedFontSize: 14,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.featured_play_list), title: Text('首页')),
          BottomNavigationBarItem(icon: Icon(Icons.play_circle_filled), title: Text('视频')),
          BottomNavigationBarItem(icon: Icon(Icons.beach_access), title: Text('圈子')),
          BottomNavigationBarItem(icon: Icon(Icons.person_outline), title: Text('我的')),
        ],
        currentIndex: _pageIndex,
        onTap: _switchBarItem,
        showUnselectedLabels: true,
        unselectedItemColor: MyColor.TEXT_GRAY,
        selectedItemColor: MyColor.STYLE_RED,
        backgroundColor: Colors.white,
      ),
    );
  }

  void _switchBarItem(int index) {
    _pageController.jumpToPage(index);
    setState(() {
      _pageIndex = index;
    });
  }
}
